package com.xzx.masterslave.masterslave.utils;

import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 日期处理
 */
public class DateUtils {
    private final static Logger logger = LoggerFactory.getLogger(DateUtils.class);
    
    /**
     * yyyy年MM月dd日HH时mm分ss秒 
     */
    public static String format1 = "yyyy年MM月dd日HH时mm分ss秒 ";
    /**
     * yyyy年MM月dd日
     */
    public static String format2 = "yyyy年MM月dd日";
    /**
     * hh时mm分ss秒
     */
    public static String format3 = "hh时mm分ss秒";
    /**
     * yyyy-MM-dd HH:mm:ss
     */
    public static String format4 = "yyyy-MM-dd HH:mm:ss";
    /**
     * yyyy-MM-dd
     */
    public static String format5 = "yyyy-MM-dd";
    /**
     * HH:mm:ss
     */
    public static String format6 = "HH:mm:ss";
    /**
     * yyyyMMddHHmmss
     */
    public static String format7 = "yyyyMMddHHmmss";
    /**
     * yyyyMMdd
     */
    public static String format8 = "yyyyMMdd";
    /**
     * HHmmss
     */
    public static String format9 = "HHmmss";
    /**
     * yyyyMM
     */
    public static String format10 = "yyyyMM";
    /**
     * 正则表达式校验日期格式
     */
    public static String commonDatePattern = "^[0-9]{4}-[0-9]{2}-[0-9]{2}$";
    
    /**
     *
     * 功能描述: <br>
     * 〈当前日期/时间 yyyyMMdd〉
     *
     * @return
     */
    public static String getCurrentTime( ) {
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat(format8);
        String sdate = sdf.format(d);
        return sdate;
    }
    
    public static String getCurrentSecond() {
        return getCurrentDate(format4);
    }
    
    public static Date parseDate(String date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            return sdf.parse(date);
        } catch (ParseException e) {
            logger.error(ExceptionUtils.getFullStackTrace(e));
            return null;
        }
    }
    /**
     * 
     * 功能描述: <br>
     * 格式化日期
     *
     * @param date
     * @param format
     * @return
     */
    public static String formatDate(Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        String sdate = sdf.format(date);
        return sdate;
    }
    /**
     * 功能描述: <br>
     * 〈当前日期/时间〉
     * @param format 日期格式(默认：yyyyMMdd
     */
    public static String getCurrentTime(String format) {
        if (StringUtils.isEmpty(format)) {
            format = format8;
        }
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        String sdate = sdf.format(d);
        return sdate;
    }
    
    
    public static String getCurrentDate() {
        return getCurrentTime(format5);
    }
    
    public static String getCurrentDate(String format) {
        return getCurrentTime(format);
    }
    /**
     * 时间格式(yyyy-MM-dd)
     */
    public final static String DATE_PATTERN = "yyyy-MM-dd";
    /**
     * 时间格式(yyyy-MM-dd HH:mm:ss)
     */
    public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

    public static String format(Date date) {
        return format(date, DATE_PATTERN);
    }

    public static String format(Date date, String pattern) {
        if (date != null) {
            SimpleDateFormat df = new SimpleDateFormat(pattern);
            return df.format(date);
        }
        return null;
    }

    /**
     * 计算距离现在多久，非精确
     *
     * @param date
     * @return
     */
    public static String getTimeBefore(Date date) {
        Date now = new Date();
        long l = now.getTime() - date.getTime();
        long day = l / (24 * 60 * 60 * 1000);
        long hour = (l / (60 * 60 * 1000) - day * 24);
        long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
        long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        String r = "";
        if (day > 0) {
            r += day + "天";
        } else if (hour > 0) {
            r += hour + "小时";
        } else if (min > 0) {
            r += min + "分";
        } else if (s > 0) {
            r += s + "秒";
        }
        r += "前";
        return r;
    }

    /**
     * 计算距离现在多久，精确
     *
     * @param date
     * @return
     */
    public static String getTimeBeforeAccurate(Date date) {
        Date now = new Date();
        long l = now.getTime() - date.getTime();
        long day = l / (24 * 60 * 60 * 1000);
        long hour = (l / (60 * 60 * 1000) - day * 24);
        long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
        long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        String r = "";
        if (day > 0) {
            r += day + "天";
        }
        if (hour > 0) {
            r += hour + "小时";
        }
        if (min > 0) {
            r += min + "分";
        }
        if (s > 0) {
            r += s + "秒";
        }
        r += "前";
        return r;
    }
}
